package com.xique.common.core.utils.command;

import com.xique.common.core.constant.CommandResultCode;
import com.xique.common.core.web.domain.AjaxResult;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

/**
 * 命令下发结果处理
 *
 * @author WLD
 */
@UtilityClass
@Slf4j
public class CommandResultUtil {

    private static final String RESULT_CODE = "result_code";

    private static final String MESSAGE = "message";

    public static AjaxResult handleCommandResult(AjaxResult commandResult, String otherMsg) {
        log.info("下发结果：{}", commandResult);
        if (null != commandResult) {
            if (null != commandResult.get(RESULT_CODE)) {
                Integer resultCode = (Integer) commandResult.get(RESULT_CODE);
                String message = "命令下发结果获取失败";
                if (null != commandResult.get(MESSAGE)) {
                    message = (String) commandResult.get(MESSAGE);
                }
                if (CommandResultCode.OK == resultCode) {
                    return AjaxResult.success(otherMsg + message);
                } else if (CommandResultCode.REQ_RSP == resultCode || CommandResultCode.REQ_NORSP == resultCode) {
                    return AjaxResult.success(otherMsg + "命令下发结果：" + message);
                } else {
                    return AjaxResult.commandError(otherMsg + "命令下发失败，失败原因：" + message);
                }
            }
        }
        return AjaxResult.commandError(otherMsg + "命令下发结果获取失败");
    }

}
